{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdf6664d-8193-4491-98a4-8a6de371fe0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 读取数据\n",
    "data = pd.read_csv('examdata.csv')\n",
    "data.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1a98ba7-33fb-49f1-a284-6125111efd82",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可视化数据\n",
    "# 启用嵌入显示功能\n",
    "\n",
    "%matplotlib inline \n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "fig1 = plt.figure()\n",
    "e1 = data.loc[:,'Exam1']\n",
    "e2 = data.loc[:,'Exam2']\n",
    "\n",
    "plt.scatter(e1, e2, c = 'hotpink')\n",
    "plt.title('Exam1 - Exam2')\n",
    "plt.xlabel('Exam1')\n",
    "plt.ylabel('Exam2')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce5d8a66-3f05-43fa-ab2b-7d969a23c725",
   "metadata": {},
   "outputs": [],
   "source": [
    "# add lable mask\n",
    "mask = data.loc[:,'Pass'] == 1\n",
    "print(mask)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d72f6205-12fb-4b32-88b5-44370d4c786f",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig2 = plt.figure(facecolor='w')\n",
    "\n",
    "plt.gca().set_facecolor('black') \n",
    "# 根据上面的标签mask来区分不同的点，如果不给出颜色，也是自动换成连个颜色的\n",
    "passed = plt.scatter(data.loc[:,'Exam1'][mask], data.loc[:,'Exam2'][mask], c = 'green')\n",
    "failed = plt.scatter(data.loc[:,'Exam1'][~mask], data.loc[:,'Exam2'][~mask], c = 'pink')\n",
    "plt.legend((passed,failed),('passed','failed'))\n",
    "\n",
    "plt.title('Exam1(X-Label) - Exam2(Y-Label)')\n",
    "plt.xlabel('Exam1')\n",
    "plt.ylabel('Exam2')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01000053-f27c-4c08-9274-df1dc2fcc3e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义数据，用于训练\n",
    "\n",
    "X = data.drop(['Pass'],axis = 1) # 从data中剥离结果\n",
    "y = data.loc[:,'Pass']\n",
    "\n",
    "X1 = data.loc[:,'Exam1']\n",
    "X2 = data.loc[:,'Exam2']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb8c71c0-87f8-4824-b9ac-82f0fad304ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(X.shape, y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ae71140-c32e-4ee0-802e-d0b592ca0375",
   "metadata": {},
   "outputs": [],
   "source": [
    "# establish the model and train it\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "LR = LogisticRegression()\n",
    "print(X.shape)\n",
    "LR.fit(X,y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf3d2d0b-813a-452c-b41b-7d1470271fb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#show predicted result and its accuracy\n",
    "y_predict = LR.predict(X)\n",
    "print(y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14a8fba5-6a6d-474a-ab62-0761725c5ae7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "accuracy = accuracy_score(y, y_predict)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50912564-36a8-475e-9162-1f22d8386f98",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exam1 70, Exam2 65\n",
    "x_test = np.array([70,65]).reshape(1,-1)\n",
    "\n",
    "print(x_test.shape)\n",
    "\n",
    "y_test = LR.predict(x_test)\n",
    "print('passed' if y_test == 1 else 'failed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2b7f196-bd7a-43cb-af58-f42e1a7d99dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "LR.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67dcb925-5e8b-45bf-bca4-05c78940b49f",
   "metadata": {},
   "outputs": [],
   "source": [
    "LR.intercept_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3254f1b8-dbc0-4387-afdd-f7c992c7e3bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "theta0 = LR.intercept_\n",
    "theta1 = LR.coef_[0][0]\n",
    "theta2 = LR.coef_[0][1]\n",
    "\n",
    "print(theta0, theta1, theta2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8eeed39-86c0-4dbe-8dcb-187b7e86bcba",
   "metadata": {},
   "outputs": [],
   "source": [
    "X2_NEW = (-theta0 - theta1 * X1)/theta2\n",
    "print(X2_NEW)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "937682a1-ae27-4fa6-9f67-ef52dbb7a42e",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig3 = plt.figure()\n",
    "plt.plot(X1, X2_NEW)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd7fb184-1538-487b-b3b9-2b79d3dd68b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "theta0 = LR.intercept_\n",
    "theta1 = LR.coef_[0][0]\n",
    "theta2 = LR.coef_[0][1]\n",
    "X1_NEW = (-theta0 - theta2 * X2)/theta1\n",
    "\n",
    "fig4 = plt.figure()\n",
    "plt.plot(X1_NEW, X2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c57ec86b-2e4d-4bb1-809b-7976d94ba011",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f3d00cb-127b-41f4-aef4-19bb6edffa8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig5 = plt.figure()\n",
    "plt.plot(X1, X2_NEW)\n",
    "\n",
    "passed = plt.scatter(data.loc[:,'Exam1'][mask], data.loc[:,'Exam2'][mask])\n",
    "failed = plt.scatter(data.loc[:,'Exam1'][~mask], data.loc[:,'Exam2'][~mask])\n",
    "plt.title('Exam1 - Exam2')\n",
    "plt.xlabel('Exam1')\n",
    "plt.ylabel('Exam2')\n",
    "plt.legend((passed,failed),('passed','failed'))\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ed70fc3-dfd7-4132-abfc-0b8790e00a85",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "251610aa-37eb-436d-8627-a0266355da73",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91ffc24a-88a0-417f-8bff-cfdb30aa9a94",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 二级边界函数\n",
    "# 创建新的数据\n",
    "\n",
    "X1_2 = X1 * X1\n",
    "X2_2 = X2 * X2\n",
    "X1_X2 = X1 * X2\n",
    "X_new = {'X1':X1,'X2':X2,'X1_2':X1_2,'X2_2':X2_2,'X1_X2':X1_X2}\n",
    "X_new = pd.DataFrame(X_new)\n",
    "print(X_new)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e52ebc2-449e-4fe3-8c89-803dac19c649",
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建新的模型\n",
    "LR2 = LogisticRegression()\n",
    "LR2.fit(X_new, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f826b79-0058-4bf5-b611-928c921852ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "y2_predict = LR2.predict(X_new)\n",
    "print(y2_predict)\n",
    "\n",
    "from sklearn.metrics import accuracy_score\n",
    "accuracy2 = accuracy_score(y, y2_predict)\n",
    "print(accuracy2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f158ca12-6da3-4ffa-9084-e63b83ce4aa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可视化\n",
    "LR2.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d6a6ac7-d946-4ae3-b430-7348386d0bf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "theta0 = LR2.intercept_\n",
    "theta1 = LR2.coef_[0][0]\n",
    "theta2 = LR2.coef_[0][1]\n",
    "theta3 = LR2.coef_[0][2]\n",
    "theta4 = LR2.coef_[0][3]\n",
    "theta5 = LR2.coef_[0][4]\n",
    "print(theta0,theta1,theta2,theta3,theta4,theta5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e002d29e-cae6-4682-b738-5816f0eaaf0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "X2_sort = X2.sort_values()\n",
    "a = theta3\n",
    "b = theta1+theta5*X2_sort\n",
    "c = theta0+theta2*X2_sort+theta4*X2_sort*X2_sort \n",
    "x1_new_boundary = (-b+np.sqrt(b*b-4*a*c))/(2*a)\n",
    "x1_new_boundary.head()\n",
    "fig6 = plt.figure()\n",
    "plt.plot(x1_new_boundary,X2_sort)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a85802da-8602-4827-9943-25aa8e879e40",
   "metadata": {},
   "outputs": [],
   "source": [
    "X1_sort = X1.sort_values()\n",
    "\n",
    "a = theta4\n",
    "b = theta5 * X1_sort + theta2\n",
    "c = theta0 + theta1 * X1_sort + theta3 * X1_sort * X1_sort\n",
    "X2_new_boundary = (-b+np.sqrt(b*b-4*a*c))/(2*a)\n",
    "\n",
    "fig7 = plt.figure()\n",
    "print(X1, X1_sort)\n",
    "plt.plot(X1_sort,X2_new_boundary)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bc23a85-89d2-4c93-8a2b-19f406de69db",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig8 = plt.figure()\n",
    "\n",
    "\n",
    "X1_sort = X1.sort_values()\n",
    "a = theta4\n",
    "b = theta5 * X1_sort + theta2\n",
    "c = theta0 + theta1 * X1_sort + theta3 * X1_sort * X1_sort\n",
    "X2_new_boundary = (-b+np.sqrt(b*b-4*a*c))/(2*a)\n",
    "\n",
    "passed = plt.scatter(data.loc[:,'Exam1'][mask], data.loc[:,'Exam2'][mask],c='green',s=5)\n",
    "failed = plt.scatter(data.loc[:,'Exam1'][~mask], data.loc[:,'Exam2'][~mask],c='pink',s=5)\n",
    "plt.title('Exam1 - Exam2')\n",
    "plt.xlabel('Exam1')\n",
    "plt.ylabel('Exam2')\n",
    "plt.legend((passed,failed),('passed','failed'))\n",
    "plt.plot(X1_sort,X2_new_boundary,c='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f55c2010-1cd6-4f1a-a320-7bb619ad84b9",
   "metadata": {},
   "source": [
    "* 边界函数\n",
    "$$\n",
    "w_1 x_1 + w_2 x_2 + b = 0\n",
    "$$\n",
    "\n",
    "* 二级边界函数\n",
    "$$\n",
    "\\theta_0 + \\theta_1 x_1 + \\theta_2 x_2 + \\theta_3 x_1^2 + \\theta_4 x_2^2 + \\theta_5 x_1 x_2 = 0\n",
    "$$\n",
    "\n",
    "* 二阶边界函数的标准形式\n",
    "$$\n",
    "\\theta_3 x_1^2 + (\\theta_1 + \\theta_5 x_2) x_1 + (\\theta_0 + \\theta_2 x_2 + \\theta_4 x_2^2) = 0\n",
    "$$\n",
    "\n",
    "* 求解x\n",
    "$$\n",
    "x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n",
    "$$\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ad4aa4c-5547-4dfb-bc93-43080d9a0ef0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1856a05a-442f-4731-a815-c0930f52b372",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42b9ceb3-f172-43df-9e86-8b7ec6c98394",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26a0c285-a5c4-4426-80b8-0ee3e5d9d391",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e79c9a32-5034-4c48-af14-0ef5abb97cdf",
   "metadata": {},
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04f60036-2a6e-4be1-9172-d33293ae1d45",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18df5483-13b8-4d3a-8928-bfbdf7aa3b2a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "232054dd-820c-4381-b327-9aa77e693f78",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce6baf49-c4ab-4f1f-850e-53486d548008",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7cb2c877-9f19-45a5-bdaa-881b0c3e21cb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a948354-2821-44d6-84df-2509db58a2fd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dad21093-e715-4b54-af25-9bcaaa93e430",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fe60bff-afdc-4db8-aab2-e0fcca38324a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "847f6d6a-1042-4b65-8fff-12b1a20c7e30",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81ff76f8-7550-4690-89dd-99a03a945648",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "048f6771-a2b7-46f7-9767-4a448d79b46e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80d48017-1f11-422f-93be-8a93f01a6a42",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b562415-a9ba-4b42-97e5-9671b3ce4cef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de88657e-3a43-4ac3-8d97-1cb18470a079",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "035d3508-06a2-45b1-bb11-d72bf17338cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
